package cn.mes.module.mes.controller.admin.basecustomerinfo;

import cn.mes.framework.apilog.core.annotation.ApiAccessLog;
import cn.mes.framework.common.pojo.CommonResult;
import cn.mes.framework.common.pojo.PageResult;
import cn.mes.framework.common.pojo.PageParam;
import cn.mes.framework.common.util.object.BeanUtils;
import cn.mes.framework.excel.core.util.ExcelUtils;
import cn.mes.module.mes.controller.admin.basecustomerinfo.vo.BaseCustomerInfoPageReqVO;
import cn.mes.module.mes.controller.admin.basecustomerinfo.vo.BaseCustomerInfoRespVO;
import cn.mes.module.mes.controller.admin.basecustomerinfo.vo.BaseCustomerInfoSaveReqVO;
import cn.mes.module.mes.dal.dataobject.basecustomerinfo.BaseCustomerInfoDO;
import cn.mes.module.mes.service.basecustomerinfo.BaseCustomerInfoService;
import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;
import java.io.IOException;

import static cn.mes.framework.common.pojo.CommonResult.success;

import static cn.mes.framework.apilog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 客户基础信息")
@RestController
@RequestMapping("/mes/base-customer-info")
@Validated
public class BaseCustomerInfoController {

    @Resource
    private BaseCustomerInfoService baseCustomerInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建客户基础信息")
    @PreAuthorize("@ss.hasPermission('mes:base-customer-info:create')")
    public CommonResult<String> createBaseCustomerInfo(@Valid @RequestBody BaseCustomerInfoSaveReqVO createReqVO) {
        return success(baseCustomerInfoService.createBaseCustomerInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户基础信息")
    @PreAuthorize("@ss.hasPermission('mes:base-customer-info:update')")
    public CommonResult<Boolean> updateBaseCustomerInfo(@Valid @RequestBody BaseCustomerInfoSaveReqVO updateReqVO) {
        baseCustomerInfoService.updateBaseCustomerInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户基础信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:base-customer-info:delete')")
    public CommonResult<Boolean> deleteBaseCustomerInfo(@RequestParam("id") String id) {
        baseCustomerInfoService.deleteBaseCustomerInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户基础信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:base-customer-info:query')")
    public CommonResult<BaseCustomerInfoRespVO> getBaseCustomerInfo(@RequestParam("id") String id) {
        BaseCustomerInfoDO baseCustomerInfo = baseCustomerInfoService.getBaseCustomerInfo(id);
        return success(BeanUtils.toBean(baseCustomerInfo, BaseCustomerInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户基础信息分页")
    @PreAuthorize("@ss.hasPermission('mes:base-customer-info:query')")
    public CommonResult<PageResult<BaseCustomerInfoRespVO>> getBaseCustomerInfoPage(@Valid BaseCustomerInfoPageReqVO pageReqVO) {
        PageResult<BaseCustomerInfoDO> pageResult = baseCustomerInfoService.getBaseCustomerInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseCustomerInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户基础信息 Excel")
    @PreAuthorize("@ss.hasPermission('mes:base-customer-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseCustomerInfoExcel(@Valid BaseCustomerInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseCustomerInfoDO> list = baseCustomerInfoService.getBaseCustomerInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户基础信息.xls", "数据", BaseCustomerInfoRespVO.class,
                        BeanUtils.toBean(list, BaseCustomerInfoRespVO.class));
    }

}